package com.be.beadmin.design.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.design.dto.output.BomReviewMainExcelEx;
import com.be.beadmin.design.entity.BomReviewMain;
import com.be.beadmin.design.dto.output.BomReviewMainEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.design.service.BomReviewMainService;
import com.be.beadmin.design.dto.input.BomReviewMainQueryCriteria;
import com.be.beadmin.design.dto.input.BomReviewMainDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zhouz
* @date 2023-11-10
**/
@Api(tags = {"设计管理-Bom"})
@RestController
public class BomReviewMainController {

    @Autowired
    private  BomReviewMainService bomReviewMainService;

    @ApiOperation("getBomReviewMain")
    @PostMapping(value = "/design/getBomReviewMainByPage")
    public ResultModel getBomReviewMainByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(bomReviewMainService.getBomReviewMainByPage(pageEntity)) ;
    }

    @PostMapping("/design/addBomReviewMain")
    public ResultModel addBomReviewMain(@RequestBody BomReviewMainDto bomReviewMainDto){
        bomReviewMainService.addBomReviewMain(bomReviewMainDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/design/delBomReviewMain")
    public ResultModel delBomReviewMain(@RequestBody BomReviewMainDto bomReviewMainDto){
        bomReviewMainService.delBomReviewMain(bomReviewMainDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/design/updateBomReviewMain")
    public ResultModel updateBomReviewMain(@RequestBody BomReviewMainDto bomReviewMainDto){
        bomReviewMainService.updateBomReviewMain(bomReviewMainDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/design/exportBomReviewMain")
    public void exportBomReviewMain(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<BomReviewMainEx> bomReviewMainPage = bomReviewMainService.getBomReviewMainByPage(pageEntity);
        List<BomReviewMainEx> bomReviewMainExes = bomReviewMainPage.getContent();
        List<BomReviewMainExcelEx> bomReviewMainExcelExes=bomReviewMainExes.stream().map(item->{
            BomReviewMainExcelEx excel=new BomReviewMainExcelEx();
            BeanUtils.copyProperties(item,excel);
            return  excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("BomReviewMain接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), BomReviewMainExcelEx.class).sheet("sheet1").doWrite(bomReviewMainExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), BomReviewMainExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(bomReviewMainExcelExes);
    }
}